package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;

import tags.*;
import tags.exceptions.NotSupportedFormatException;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import entagged.audioformats.exceptions.CannotReadException;

import java.util.Hashtable;
import java.util.Iterator;
import java.awt.Component;

/**
 * <p>
 * Title: Test GUI
 * </p>
 *
 * @author alexog
 * @version 1.0
 */
public class TagFrame extends JFrame {
    BorderLayout borderLayout = new BorderLayout();
    
    JMenuBar menuBar = new JMenuBar();
    
    JMenu fileMenu = new JMenu();
    
    JMenuItem openmenu = new JMenuItem();
    
    JTable tagTable = null;
    
    DefaultTableModel model = new DefaultTableModel();
    
    DefaultListModel listmodel = new DefaultListModel();
    
    public TagFrame() {
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    
    private void jbInit() throws Exception {
        model.addColumn("Tag");
        model.addColumn("Value");
        tagTable = new JTable(model);
        getContentPane().setLayout(borderLayout);
        fileList = new MyList(listmodel);
        fileList.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (fileList.getSelectedValue() != null) {
                    model.setRowCount(0);
                    tagFile(new File((String) fileList.getSelectedValue()));
                }
            }
        });
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setJMenuBar(menuBar);
        setVisible(true);
        fileMenu.setText("File");
        openmenu.setText("Open");
        
        openmenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int returnVal = chooser.showOpenDialog(getFrame());
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File f = chooser.getSelectedFile();
                    if (f != null) {
                        listmodel.addElement(f.getAbsolutePath());
                        tagFile(f);
                    }
                }
            }
        });
        
        tagTable.setEnabled(false);
        closemenu.setText("Close");
        closemenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (fileList.getSelectedValue() != null) {
                    listmodel.removeElement(fileList.getSelectedValue());
                }
            }
        });
        viewMenu.setText("View");
        vieFileMenu.setText("ViewFile");
        vieFileMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (fileList.getSelectedValue() != null) {
                    model.setRowCount(0);
                    tagFile(new File((String) fileList.getSelectedValue()));
                }
            }
        });
        clearMenu.setText("Cleartable");
        clearMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                model.setRowCount(0);
                tagTable.repaint();
            }
        });
        exitMenu.setText("Exit");
        exitMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                dispose();
            }
        });
        menuBar.add(fileMenu);
        menuBar.add(viewMenu);
        fileMenu.add(openmenu);
        fileMenu.add(closemenu);
        fileMenu.addSeparator();
        fileMenu.add(exitMenu);
        listScrollPane.getViewport().add(fileList);
        viewMenu.add(vieFileMenu);
        viewMenu.add(clearMenu);
        getContentPane().add(listScrollPane, java.awt.BorderLayout.NORTH);
        getContentPane().add(tagScrollPane, java.awt.BorderLayout.CENTER);
        
        tagScrollPane.getViewport().add(tagTable);
        
    }
    
    public Component getFrame() {
        return this;
    }
    
    public static void main(String[] args) {
        TagFrame tagframe = new TagFrame();
        tagframe.setSize(600, 400);
        tagframe.setDefaultCloseOperation(EXIT_ON_CLOSE);
        tagframe.setVisible(true);
    }
    
    JFileChooser chooser = new javax.swing.JFileChooser();
    
    JScrollPane listScrollPane = new JScrollPane();
    
    MyList fileList;
    
    JMenuItem closemenu = new JMenuItem();
    
    JMenu viewMenu = new JMenu();
    
    JMenuItem vieFileMenu = new JMenuItem();
    
    JMenuItem clearMenu = new JMenuItem();
    
    JMenuItem exitMenu = new JMenuItem();
    
    JScrollPane tagScrollPane = new JScrollPane();
    
    /**
     * tagFile add to tagtable tags
     *
     * @param f File
     */
    private void tagFile(File f) {
        TagReader reader = null;
        try {
            reader = new TagReader(f);
            model.setRowCount(0);
            
            Iterator it = reader.getFields();
            
            while (it.hasNext()){
                Map.Entry entry = (Map.Entry) it.next();
                model.addRow(new Object[] { entry.getKey(), entry.getValue() });
            }
//            Hashtable map = reader.getTagList();
//            Iterator it = null;
//            it = map.keySet().iterator();
//            while (it.hasNext()) {
//                Object key = it.next();
//                Object val = map.get(key);
//                model.addRow(new Object[] { key, val });
//            }
            tagTable.repaint();
        } catch (FileNotFoundException e) {
            model.addRow(new Object[] {"Error",e.getMessage()});
            tagTable.repaint();
        } catch (NotSupportedFormatException e) {
            model.addRow(new Object[] {"Error",e.getMessage()});
            tagTable.repaint();
            e.printStackTrace();
        } catch (IOException e) {
            model.addRow(new Object[] {"Error",e.getMessage()});
            tagTable.repaint();
            e.printStackTrace();
        } catch (CannotReadException e) {
            model.addRow(new Object[] {"Error",e.getMessage()});
            tagTable.repaint();
            e.printStackTrace();
        }
    }
}
